package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.mpandroidchart.MpChartHelper;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.HistoryTcV3;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.HistoryTcVT;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.NewHistoryTcPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.INewHistoryTc;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DateUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainUdis;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 作者：Administrator on 2017/7/1 15:15
 * 邮箱：zhanghuaiha@gmail.com
 *  历史明细
 */

public class NewHistoryTcActivity extends BaseActivity<INewHistoryTc,NewHistoryTcPresenter> implements INewHistoryTc {
    private LineChart mChart;
    private ImageView back;
    private TextView title,start,end,sta_date,icon;
    private RecyclerView top;
    private TimePickerView pvTime;
    private String udid;
    private static final String TAG = NewHistoryTcActivity.class.getSimpleName();

    @Override
    public void initView() {
        mChart = (LineChart) findViewById(R.id.sta_chart);
        back = (ImageView) findViewById(R.id.sta_back);
        icon = (TextView) findViewById(R.id.sta_text);
        title = (TextView) findViewById(R.id.sta_title);
        start = (TextView) findViewById(R.id.sta_start);
        end = (TextView) findViewById(R.id.sta_end);
        top = (RecyclerView) findViewById(R.id.sta_top);
        sta_date = (TextView) findViewById(R.id.sta_date);
        end.setText(DateUtils.getNextDay(System.currentTimeMillis(),"yyyy-MM-dd",1));
        start.setText(DateUtils.getNextDay(System.currentTimeMillis(),"yyyy-MM-dd",8));
        title.setText("新历史明细");
        initTimePicker();
    }

    @Override
    public void initData() {
        udid = ObtainUdis.getUDID(NewHistoryTcActivity.class);
        mPresenter.loadContacts(udid);
    }

    @Override
    public void initListener() {
        String msg = "提示0\r\n今日总提成\r\n提示1\r\n涨粉提成\r\n提示2\r\n支付提成\r\n提示3\r\n纸巾机支付金额" +
                "\r\n提示4\r\n纸巾机支付笔数\r\n提示5\r\n密码设备支付金额\r\n提示6\r\n密码设备支付笔数\r\n提示7\r\n纸巾机涨粉\r\n提示8" +
                "\r\n密码设备涨粉\r\n提示8\r\nwifi设备涨粉";
        start.setOnClickListener(v -> pvTime.show(v));
        end.setOnClickListener(v -> pvTime.show(v));
//        icon.setOnClickListener(v -> mPresenter.Dialog("提示",msg));
        icon.setOnClickListener(v -> jumpToActivity(HistoryTcActivity.class));
        back.setOnClickListener(v -> finish());
        sta_date.setOnClickListener(v -> {
            if (top.getVisibility() == View.INVISIBLE) {
                top.setVisibility(View.VISIBLE);
            }else {
                top.setVisibility(View.INVISIBLE);
            }
        });
    }



    public void showDialog(){
      //  mPresenter.Dialog("日均涨粉："+zhangfen,"设备使用率："+shiyong);
    }

    //显示隐藏某个线条
    public void visi(int position){
        List<ILineDataSet> dataSets = mChart.getData().getDataSets();
        if (dataSets.get(position).isVisible()) {
            dataSets.get(position).setVisible(false);
        }else {
            dataSets.get(position).setVisible(true);
        }
        mChart.invalidate();
    }

    private List<List<Float>> yAxisValues;
    private List<String> titles;
    private List<String> xAxisValues;

    public static final int[] PIE_COLORS = {
            Color.parseColor("#5fa1c4"),Color.parseColor("#5f79c4"), Color.parseColor("#c45f5f"),
            Color.parseColor("#b25fc4"), Color.parseColor("#9dc45f"), Color.parseColor("#805fc487"),
            Color.parseColor("#80c4b05f"), Color.parseColor("#80c4190f"),Color.parseColor("#80c4190f"),Color.parseColor("#80c4190f")
    };


    // 这里是初始化线条的x y值 所以这里可以自己实现根据实际数据
    public void setData(List<HistoryTcV3> data) {
        if (data.size() <= 1) {
            DialogUtils.TSDialog(NewHistoryTcActivity.this,"暂无数据",true);
            return;
        }
        mChart.clear();
        mChart.setData(new LineData());
        mChart.invalidate();

        yAxisValues = new ArrayList<>();
        xAxisValues = new ArrayList<>();
        titles = new ArrayList<>();


        //今日总提成 （默认显示这个）
        List<Float> yAxisValues1 = new ArrayList<>();
        //涨粉提成
        List<Float> yAxisValues2 = new ArrayList<>();
        //支付提成
        List<Float> yAxisValues3 = new ArrayList<>();
        //纸巾机支付金额
        List<Float> yAxisValues4 = new ArrayList<>();
        //纸巾机支付笔数
        List<Float> yAxisValues5 = new ArrayList<>();
        //密码设备支付金额
        List<Float> yAxisValues6 = new ArrayList<>();
        //密码设备支付笔数
        List<Float> yAxisValues7 = new ArrayList<>();
        //纸巾机涨粉
        List<Float> yAxisValues8 = new ArrayList<>();
        //密码设备涨粉
        List<Float> yAxisValues9 = new ArrayList<>();
        //wifi设备涨粉
        List<Float> yAxisValues0 = new ArrayList<>();


        for (int i = 0; i < data.size(); i++) {
            xAxisValues.add(String.valueOf(i));
            yAxisValues1.add(Float.parseFloat(data.get(i).getMerge_commission()));
            yAxisValues2.add(Float.parseFloat(data.get(i).getSub_count_commission()));
            yAxisValues3.add(Float.parseFloat(data.get(i).getPay_commission()));
            yAxisValues4.add(Float.parseFloat(data.get(i).getPaper_pay_money()));
            yAxisValues5.add(Float.parseFloat(data.get(i).getPaper_pay_count()));
            yAxisValues6.add(Float.parseFloat(data.get(i).getMima_charge_pay_money()));
            yAxisValues7.add(Float.parseFloat(data.get(i).getMima_charge_pay_count()));
            yAxisValues8.add(Float.parseFloat(data.get(i).getPaper_sub_count()));
            yAxisValues9.add(Float.parseFloat(data.get(i).getMima_charge_sub_count()));
            yAxisValues0.add(Float.parseFloat(data.get(i).getWifi_charge_sub_count()));
        }

        yAxisValues.add(yAxisValues1);
        yAxisValues.add(yAxisValues2);
        yAxisValues.add(yAxisValues3);
        yAxisValues.add(yAxisValues4);
        yAxisValues.add(yAxisValues5);
        yAxisValues.add(yAxisValues6);
        yAxisValues.add(yAxisValues7);
        yAxisValues.add(yAxisValues8);
        yAxisValues.add(yAxisValues9);
        yAxisValues.add(yAxisValues0);


        titles.add("今日总提成");
        titles.add("涨粉提成");
        titles.add("支付提成");
        titles.add("纸巾机支付金额");
        titles.add("纸巾机支付笔数");
        titles.add("密码设备支付金额");
        titles.add("密码设备支付笔数");
        titles.add("纸巾机涨粉");
        titles.add("密码设备涨粉");
        titles.add("wifi设备涨粉");

        MpChartHelper.setLinesChart(mChart, xAxisValues, yAxisValues, titles, true, PIE_COLORS);
        mChart.notifyDataSetChanged();
    }

    /**
     * 初始化时间选着器
     */
    private void initTimePicker() {

        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                long start_time = date.getTime();
                long end_time = System.currentTimeMillis();
                if (start_time > end_time) {
                    ToastUtil.showToast("选择日期不能大于当前时间");
                    return;
                }
                String time = getTime(date);
                TextView textView = (TextView) v;
                //开始时间变化
                if (textView.getId() == R.id.sta_start) {
                    long time_long = getTime_long(end.getText().toString());
                    Log.e(TAG,"start被点击了"+time_long);
                    if (start_time < time_long) {
                        mPresenter.loadNetWork(udid,time,end.getText().toString());
                    }else {
                        ToastUtil.showToast("开始时间大于结束时间");
                        return;
                    }

                }else if (textView.getId() == R.id.sta_end) {
                    long time_long = getTime_long(start.getText().toString());
                    if (time_long < start_time) {
                        mPresenter.loadNetWork(udid,start.getText().toString(),time);
                    }else {
                        ToastUtil.showToast("结束时间必须大于开始时间");
                    }
                }
                textView.setText(time);


            }
        })
                .build();
        pvTime.setDate(Calendar.getInstance());
    }
    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private long getTime_long(String time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return  format.parse(time).getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    protected NewHistoryTcPresenter createPresenter() {
        return new NewHistoryTcPresenter(this);
    }


    @Override
    protected int provideContentViewId() {
        return R.layout.activity_new_statistics;
    }


    @Override
    public RecyclerView getTop() {
        return top;
    }

    @Override
    public NewHistoryTcActivity getAc() {
        return NewHistoryTcActivity.this;
    }

    @Override
    public TextView getStart() {
        return start;
    }

    @Override
    public TextView getEnd() {
        return end;
    }
}
